package controller;


import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Books;
import pojo.Healths;
import service.BookService;
import service.HealthService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class HealthController {

    @Autowired
    @Qualifier("HealthServiceImpl")
    private HealthService healthService;


    @ResponseBody
    @RequestMapping(value = "/allHealthPeople",produces="text/html;charset=utf-8")
    public String allHealth( HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control","no-cache");
        List<Healths> list = healthService.queryAllBook();
        String resJSON = JSON.toJSONString(list);     // 转换为json
        return resJSON;
    }

    @ResponseBody
    @RequestMapping(value = "/HealthById",produces="text/html;charset=utf-8")
    public String queryHealthById(String healthId,HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control","no-cache");
        Healths healths = healthService.queryBookById(healthId);
        String resJSON = JSON.toJSONString(healths);     // 转换为json
        return resJSON;
    }



    @RequestMapping("/allHealth")
    public String list(Model model){
        List<Healths> list = healthService.queryAllBook();
        model.addAttribute("list",list);
        System.out.println(list);
        return "healthsManager/healthsManager";
    }



    //    删除
    @RequestMapping("/delHealth/{healthId}")
    public String deleteBook(@PathVariable("healthId") String healthId){
        System.out.println(healthId);
        healthService.deleteBookById(healthId);
        return "redirect:/allHealth";
    }



}
